// miniprogram/pages/catalog/catalog.js

//引用api文件
const api = require('../../utils/api');
//请求文件
const { myRequest } = require('../../utils/myRequest');
const app =getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    catalogleft:[],//分类页面左边导航数据
    catalogright:[],//分类页面右面数据
    navindex:0,//分类页面左边导航下标
    topindex:0,//详情页顶部下标
    category:[],//详情页数据w
    page: 1,//除热门和新品外页数控制
    mpage: 1,//热门和新品外页数控制
    sty:true,//分类与详情切换
    hasdatas:true,//是否显示无数据信息
    maskarr:null,//品牌分类
    masksty:false,//分类页遮罩层
    catesty: false,//详情页遮罩层
    pinpaimsg:[],//品牌详情
    type:0,//新品或热门
    newarr:[],//新品
    hotarr: [],//热门
    re:[],
    // moveParams: {
    //   scrollLeft: 0, // scroll-view滚动的距离,默认为0,因为没有触发滚动
    //   subLeft:null, //点击元素距离屏幕左边的距离
    //   subHalfWidth:null, //点击元素的宽度一半
    //   screenHalfWidth:null //屏幕宽度的一半
    // },
    // scrollLeft:null,
  },

  //分类页左边导航点击事件 
  navclick(e){
    wx.showToast({
      title: '正在加载...',
      icon: 'loading',
      mask: true,
    })
    myRequest({
      url: api.catalogCurrent,
      data:{
        id: this.data.catalogleft[e.currentTarget.dataset.index].id,
      },
      method: 'get',
      success: res => {
        // success
        this.setData({
          navindex: e.currentTarget.dataset.index,
          catalogright: res.data.data.currentCategory.subCategoryList,
        })
        wx.showToast({
          duration:0,
          mask: true,
        })
      }
    });
  },

  //分类页右边列表点击事件 
  listclick(e){
    this.qequest(e.currentTarget.dataset.index, 1, 0)
  },

  //详情页顶部导航点击事件 
  topclick(e){
    //测试点击居中
    // let ele = '#item' + e.currentTarget.dataset.index;
    // this.getRect(ele);
    this.setData({
      category:null
    })
    this.qequest(e.currentTarget.dataset.index, 1, 0)
  },

  //详情页内容加载更多
  loadMore(e){
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      mask:true,
    })
    let page = this.data.page + 1;
    this.qequest(this.data.topindex, page, 1)
  },

  //跳转到详情页
  todetails(e){
    //跳转详情页数据
    let detailurl;
    if (this.data.type == "new") {
      // console.log("跳转详情页数据：", this.data.newarr[e.currentTarget.dataset.index]);
      detailurl = this.data.newarr[e.currentTarget.dataset.index].id
    }else if (this.data.type == "hot") {
      // console.log("跳转详情页数据：", this.data.hotarr[e.currentTarget.dataset.index]);
      detailurl = this.data.hotarr[e.currentTarget.dataset.index].id
    }else{
      // console.log("跳转详情页数据：",this.data.category[e.currentTarget.dataset.index])
      detailurl = this.data.category[e.currentTarget.dataset.index].id
    }
    wx.navigateTo({
      url: `../prodetail/prodetail?id=${detailurl}`,
    })
  },

  //相关请求
  qequest(index,page,type){
    if (this.data.masksty) {
      var up = "brandId";
      var pid = this.data.maskarr[index].id
    } else {
      var up = "categoryId";
      var pid = this.data.catalogright[index].id
    }
    myRequest({
      url: api.goodsList,
      data: {
        // [up]: this.data.catalogright[index].id,
        [up]: pid,
        page: page,
        size: 10
      },
      method: 'get',
      success: res => {
        // success
        if(type == 0){
          if (res.data.data.count == 0) {
            this.setData({
              page: 1,
              hasdatas: false,
              sty: false,
              topindex: index,
            })
          } else {
            this.setData({
              page: 1,
              hasdatas: true,
              category: res.data.data.goodsList,
              sty: false,
              topindex: index,
            })
          }
          if (this.data.masksty && this.data.catesty) {
            this.cateShow();
          }
        }else if(type == 1){
          if (res.data.data.goodsList.length == 0) {
            wx.showToast({
              title: '已经到底了！！！',
              icon: 'none',
              duration: 2500,
            });
            console.log("end!!!");
            return false;
          }
          this.data.category = this.data.category.concat(res.data.data.goodsList)
          this.setData({
            page: page,
            category: this.data.category
          })
        }

      }
    });
  },
  // 分类页品牌层展示
  maskShow(){
    this.setData({
      masksty: true,
      navindex:"pinpai",
      catalogright:null,
    })
  },
  // 分类页品牌层关闭
  maskClose(){
    this.setData({
      masksty:false,
    })
  },
  // 分类页品牌加载跟多
  maskloadMore(){
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      mask: true,
    });
    let page = this.data.page+1;
    myRequest({
      url: api.brandList,
      data: {
        page: page,
        size: 10,
      },
      method: 'get',
      success: res => {
        // success
        if (res.data.data.data.length == 0) {
          wx.showToast({
            title: '已经到底了！！！',
            icon: 'none',
            duration: 2500,
          });
          return false;
        }
        this.data.maskarr = this.data.maskarr.concat(res.data.data.data)
        this.setData({
          page: page,
          maskarr: this.data.maskarr
        })
      }
    });
  },
  // 品牌层打开
  cateShow(){
    myRequest({
      url: api.brandDetail,
      data: {
        id:this.data.maskarr[this.data.topindex].id
      },
      method: 'get',
      success: res => {
        // success
        this.setData({
          pinpaimsg: res.data.data.brand,
          catesty: true,
        })
      }
    });
  },
  // 品牌层关闭
  cateClose(){
    this.setData({
      catesty: false,
    })
  },
  // 定义新品或热门类型
  newOrhot(e){
    this.setData({
      type: e.target.dataset.type,
    })
  },
  //关闭N Or R时恢复type
  newClose(){
    this.setData({
      type: 0,
    })
  },
  //热门或新品加载
  loadnew(){
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      mask: true,
    })
    var page = this.data.mpage + 1;
    if (this.data.type=="hot") {
      var up = "isHot";
    }
    if (this.data.type == "new") {
      var up = "isNew";
    }
    myRequest({
      url: api.goodsList,
      data: {
        [up]: 1,
        page: page,
        size: 10
      },
      method: 'get',
      success: res => {
        // success
        if (res.data.data.data.length == 0) {
          wx.showToast({
            title: '已经到底了！！！',
            icon: 'none',
            duration: 2500,
          });
          return false;
        }
        if (this.data.type == "hot") {
          this.data.hotarr = this.data.hotarr.concat(res.data.data.goodsList)
          this.setData({
            mpage: page,
            hotarr: this.data.hotarr
          })
        }
        if (this.data.type == "new") {
          this.data.newarr = this.data.newarr.concat(res.data.data.goodsList)
          this.setData({
            mpage: page,
            newarr: this.data.newarr
          })
        }
      }
    });
  },


  //居中功能
  // getRect(ele) { //获取点击元素的信息,ele为传入的id
  //   var that = this;
  //   wx.createSelectorQuery().select(ele).boundingClientRect(function (rect) {
  //     let moveParams = that.data.moveParams;
  //     moveParams.subLeft = rect.left;
  //     moveParams.subHalfWidth = rect.width / 2;
  //     console.log(rect);
  //     that.moveTo();
  //   }).exec()
  // },
  // scrollMove(e) {
  //   let moveParams = this.data.moveParams;
  //   moveParams.scrollLeft = e.detail.scrollLeft;
  //   this.setData({
  //     moveParams: moveParams
  //   })
  // },
  // moveTo: function () {
  //   let res = wx.getSystemInfoSync();
  //   console.log(this.data.moveParams);
  //   let subLeft = this.data.moveParams.subLeft;
  //   let screenHalfWidth = res.screenWidth;
  //   let subHalfWidth = this.data.moveParams.subHalfWidth;
  //   let scrollLeft = this.data.moveParams.scrollLeft;

  //   let distance = subLeft + screenHalfWidth - subHalfWidth;

  //   scrollLeft = scrollLeft + distance;

  //   this.setData({
  //     scrollLeft: scrollLeft
  //   })
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(app.globalData.type)
    //分类加载
    myRequest({
      url: api.catalogIndex,
      method: 'get',
      success: res => {
        // success
        this.setData({
          catalogleft: res.data.data.categoryList,
          catalogright: res.data.data.currentCategory.subCategoryList,
          re: res.data.data.currentCategory.subCategoryList,
        })
      }
    });
    //品牌加载
    myRequest({
      url: api.brandList,
      data:{
        page:1,
        size:10,
      },
      method: 'get',
      success: res => {
        // success
        
        this.setData({
          maskarr: res.data.data.data,
          page: 1,
        })
      }
    });
    //新品加载
    myRequest({
      url: api.goodsList,
      data: {
        isNew:1,
        page: 1,
        size: 10,
      },
      method: 'get',
      success: res => {
        // success
        if (app.globalData.type) {
          this.setData({
            newarr: res.data.data.goodsList,
            type: app.globalData.type,
          })
        }else{
          this.setData({
            newarr: res.data.data.goodsList
          })
        }
      }
    });
    //热门加载
    myRequest({
      url: api.goodsList,
      data: {
        isHot: 1,
        page: 1,
        size: 10,
      },
      method: 'get',
      success: res => {
        // success
        if (app.globalData.type) {
          this.setData({
            hotarr: res.data.data.goodsList,
            type: app.globalData.type,
          })
        } else {
          this.setData({
            hotarr: res.data.data.goodsList,
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log('Catalog is Show!!!')
    if (app.globalData.type){
      this.setData({
        type: app.globalData.type,
        sty: true,
      })
    }else{
      this.setData({
        type: 0,
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // console.log('Catalog is Hide!!!')
    app.globalData.type = null;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //点击tabbar 
  onTabItemTap(item) {
    this.setData({
      sty: true,
      category: null,
      catalogright: this.data.re,//分类页面右面数据
      navindex: 0,//分类页面左边导航下标
      hasdatas: true,//是否显示无数据信息
      masksty: false,
      catesty: false,
      type:0,
    })
  }
})